// components/giftBag/giftBag.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    isShow: false,
    data_package: [],
    member_key:''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 获取红包数据
     */
    getPackage: function(member_key) {
      let self = this;
      wx.request({
        url: app.globalData.ApiUrl + '/index.php?act=member_index&op=member_new_redpackage',
        method: "POST",
        data: {
          key: member_key,
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function(res) {
          //拒绝红包时间  12小时后再请求
          if (res.data.code == 200 || res.data.code == 400) { 
            var timestamp = Date.parse(new Date()) / 1000;
            wx.setStorage({
              key: "packageTime",
              data: timestamp
            })
          }          
          if (res.data.code == 200) {          
            self.setData({
              member_key: member_key,
              isShow: true,
              data_package: res.data.datas.redpackage
            });
          }
        },
        fail: function(res) {

        }
      })
    },
    /**
     * 关闭弹框
     */
    closeDialog:function(){
     let self=this;
      self.setData({
        isShow: false,
      });
    },
    /**
     * 领取红包
     */
    gainPackage:function(){
      let self = this;
      wx.showLoading({
        title: "加载中",
      })
      wx.request({
        url: app.globalData.ApiUrl + '/index.php?act=member_index&op=member_get_new_redpackage',
        method: "POST",
        data: {
          key: self.data.member_key,
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          if (res.data.code == 200) {
            self.closeDialog();
            wx.showToast({
              title: '领取成功',
              icon: 'none'
            });            
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none'
            })
          }
        },
        fail: function (res) {
          wx.showToast({
            title: '网络请求失败',
            icon: none
          })
        }
      })
    }
  }
})